﻿using System;
using System.Collections.Generic;

namespace GT
{
    public static partial class Extensions
    {
        public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (var item in source)
                action(item);

            return source;
        }

        public static int IndexOf<TSource>(this IEnumerable<TSource> list, TSource value) where TSource : IEquatable<TSource>
        {
            return list.IndexOf<TSource>(value, EqualityComparer<TSource>.Default);
        }

        public static int IndexOf<TSource>(this IEnumerable<TSource> list, TSource value, IEqualityComparer<TSource> comparer)
        {
            int index = 0;
            foreach (var item in list)
            {
                if (comparer.Equals(item, value))
                {
                    return index;
                }
                index++;
            }
            return -1;
        }

        public static T Pop<T>(this T[] array)
        {
            T ret = array[array.Length - 1];
            Array.Resize(ref array, array.Length - 1);
            //var newArray = new List<T>();
            //(array.Length - 1).IterateIndex(i => newArray.Add(array[i]));
            //array = newArray.ToArray();
            return ret;
        }

        public static int Push<T>(this T[] array, T item)
        {
            Array.Resize(ref array, array.Length + 1);
            array[array.Length - 1] = item;
            //var newArray = new List<T>(array);
            //newArray.Add(item);
            //array = newArray.ToArray();
            return array.Length;
        }

        // Unnecessary function, Select() performs the same thing
        //
        //public static IEnumerable<TOutput> Transform<T, TOutput>(this IEnumerable<T> items, Func<T, TOutput> transform)
        //{
        //    foreach (T i in items)
        //    {
        //        yield return transform(i);
        //    }
        //}

    }
}
